
package net.atremmedia.gwt.actionjava.examples;

import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.anim.ease.Back;
import actionjava.anim.ease.Bounce;
import actionjava.anim.ease.Ease;
import actionjava.anim.ease.Elastic;
import actionjava.anim.ease.Expo;
import actionjava.display.DisplayObject;
import actionjava.display.DisplayObjectContainer;
import actionjava.events.Event.EventType;
import actionjava.events.EventListener;
import actionjava.events.tick.TickEvent;
import actionjava.geom.Color;

public abstract class AJExample
{
	private Ease[] easeTypes = new Ease[] { Back.easeOut, Bounce.easeOut, Elastic.easeOut, Expo.easeOut };

	private int id;
	private String name;
	private String src;
	private String description;
	private AJExampleOverview overview;

	private static int nextID = 0;

	//
	//
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// - - Constructors
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	//
	//

	public AJExample(String name, AJExampleOverview overview)
	{
		initialize(name, "", "", overview);
	}

	public AJExample(String name, String src, AJExampleOverview overview)
	{
		initialize(name, "", src, overview);
	}

	public AJExample(String name, String description, String src, AJExampleOverview overview)
	{
		initialize(name, description, src, overview);
	}

	private void initialize(String name, String description, String src, AJExampleOverview overview)
	{
		this.id = createId();
		this.name = name;
		this.src = src;
		this.description = description;
		this.overview = overview;
	}

	private int createId()
	{
		return ++nextID;
	}

	//
	//
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// - - Public methods
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	//
	//

	public abstract void run();

	public void onRenderTick(TickEvent event)
	{
	}

	public void destroy()
	{
		DisplayObject[] children = getChildren();
		for(int i = 0;i < children.length;i++) {
			DisplayObject child = children[i];
			if(child instanceof DisplayObjectContainer) {
				DisplayObjectContainer container = (DisplayObjectContainer) child;
				destroy(container);
			}
			child.removeAllEventListeners();
			removeChild(child);
		}
	}

	private void destroy(DisplayObjectContainer container)
	{
		DisplayObject[] children = container.getChildren();
		for(int i = 0;i < children.length;i++) {
			DisplayObject child = children[i];
			if(child instanceof DisplayObjectContainer) {
				DisplayObjectContainer container2 = (DisplayObjectContainer) child;
				destroy(container2);
			}
			child.removeAllEventListeners();
			removeChild(child);
		}
	}

	//
	//
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// - - Proxy methods
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	//
	//

	public EventListener addEventListener(EventType type, EventListener handler)
	{
		return getBase().addEventListener(type, handler);
	}

	public EventListener removeEventListener(EventType type, EventListener handler)
	{
		return getBase().removeEventListener(type, handler);
	}

	public void removeAllEventListeners()
	{
		getBase().removeAllEventListeners();
	}

	public void addChild(DisplayObject child)
	{
		getBase().addChild(child);
	}

	public DisplayObject removeChild(DisplayObject child)
	{
		return getBase().removeChild(child);
	}

	public DisplayObject[] removeAllChildren()
	{
		return getBase().removeAllChildren();
	}

	public DisplayObject[] getChildren()
	{
		return getBase().getChildren();
	}

	//
	//
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// - - Util methods
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	//
	//

	public Ease getRandomEase()
	{
		return easeTypes[(int) getRandomNumber(0, (easeTypes.length - 1))];
	}

	public double getRandomNumber(double min, double max)
	{
		return Math.floor(Math.random() * (max - min + 1) + min);
	}

	public String getRandomColor()
	{
		return Color.getRGB(Double.valueOf(getRandomNumber(0, 16581375)).intValue());
	}

	//
	//
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// - - Getters & Setters
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	//
	//

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public String getSrc()
	{
		return src;
	}

	public void setSrc(String src)
	{
		this.src = src;
	}

	public double getWidth()
	{
		return overview.getWidth();
	}

	public double getHeight()
	{
		return overview.getHeight();
	}

	public void setBGColor(String color)
	{
		overview.setBGColor(color);
	}

	public AJExampleOverview getBase()
	{
		return overview;
	}
}
